const Core = require('@alicloud/pop-core');

var client = new Core({
  accessKeyId: 'LTAI5tLWv3M88k5R4Sf6SeRQ',
  accessKeySecret: 'lEPIOQFgTooJo56n461AxcDg2687bO',
  // securityToken: '<your-sts-token>', // use STS Token
  endpoint: 'https://dns.aliyuncs.com',
  apiVersion: '2015-01-09',
});

var requestOption = {
  method: 'POST',
};

function getRecordList() {
  var params = {
    DomainName: 'gepik.top',
  };
  return new Promise((resolve, reject) => {
    client.request('DescribeDomainRecords', params, requestOption).then(
      (result) => {
        if (result) {
          resolve(result.DomainRecords.Record);
        } else {
          reject();
        }
      },
      (ex) => {
        reject(ex);
      },
    );
  });
}
function updateRecordIp(newIp) {
  return getRecordList().then((recordList) => {
    for (const record of recordList) {
      if(record.Type==='TXT') continue;
      const newRecord = {
        ...record,
        Value: newIp,
      };
      client.request('UpdateDomainRecord', newRecord, requestOption).then(
        (result) => {
          Promise.resolve(true);
        },
        (error) => {
          console.log(error.message);
        },
      );
    }
  });
}

module.exports = {
  updateRecordIp,
};
